Skip to content

InMask module

InMask

Bases: Module

A module to keep specific fields of the given data models

Example:

import synalinks
import asyncio

language_model = synalinks.LanguageModel(
    model="ollama/mistral",
)

class Document(synalinks.DataModel):
    title: str = synalinks.Field(
        description="The title of the document",
    )
    text: str = synalinks.Field(
        description="The content of the document",
    )

class Summary(synalinks.DataModel):
    summary: str = synalinks.Field(
        description="the concise summary of the document",
    )

async def main():
    inputs = synalinks.Input(data_model=Document)
    summary = await synalinks.ChainOfThought(
        data_model=Summary,
        language_model=language_model,
    )(inputs)
    masked_summary = await synalinks.InMask(
        # keep only the summary field
        mask=["summary"],
    )(summary)

    program = synalinks.Program(
        inputs=inputs,
        outputs=masked_summary,
        name="summary_generator",
        description="Generate a summary from a document",
    )

asyncio.run(main())

Parameters:

Name Type Description Default
mask list

The list of keys to keep.

None
pattern str

Optional. A regex; the fields whose key matches it are kept. At least one of mask or pattern is required.

None
name str

Optional. The name of the module.

None
description str

Optional. The description of the module.

None
trainable bool

Whether the module's variables should be trainable.

False
Source code in synalinks/src/modules/masking/in_mask.py
@synalinks_export(
    [
        "synalinks.InMask",
        "synalinks.modules.InMask",
    ]
)
class InMask(Module):
    """A module to keep specific fields of the given data models

    Example:

    ```python
    import synalinks
    import asyncio

    language_model = synalinks.LanguageModel(
        model="ollama/mistral",
    )

    class Document(synalinks.DataModel):
        title: str = synalinks.Field(
            description="The title of the document",
        )
        text: str = synalinks.Field(
            description="The content of the document",
        )

    class Summary(synalinks.DataModel):
        summary: str = synalinks.Field(
            description="the concise summary of the document",
        )

    async def main():
        inputs = synalinks.Input(data_model=Document)
        summary = await synalinks.ChainOfThought(
            data_model=Summary,
            language_model=language_model,
        )(inputs)
        masked_summary = await synalinks.InMask(
            # keep only the summary field
            mask=["summary"],
        )(summary)

        program = synalinks.Program(
            inputs=inputs,
            outputs=masked_summary,
            name="summary_generator",
            description="Generate a summary from a document",
        )

    asyncio.run(main())
    ```

    Args:
        mask (list): The list of keys to keep.
        pattern (str): Optional. A regex; the fields whose key matches it are
            kept. At least one of `mask` or `pattern` is required.
        name (str): Optional. The name of the module.
        description (str): Optional. The description of the module.
        trainable (bool): Whether the module's variables should be trainable.
    """

    def __init__(
        self,
        *,
        mask=None,
        pattern=None,
        name=None,
        description=None,
        trainable=False,
    ):
        if not (mask or pattern) or (mask and not isinstance(mask, list)):
            raise ValueError(
                "`mask` parameter should be a list of fields to keep, "
                "or a `pattern` should be given"
            )
        super().__init__(
            name=name,
            description=description,
            trainable=trainable,
        )
        self.mask = mask
        self.pattern = pattern

    async def call(self, inputs):
        outputs = tree.map_structure(
            lambda x: (
                x.in_mask(mask=self.mask, pattern=self.pattern) if x is not None else x
            ),
            inputs,
        )
        return outputs